//
//  CZStatusRetweetView.swift
//  Weibo27
//
//  Created by apple on 16/4/20.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit
class CZStatusRetweetView: UIView {
    
    // 记录我们转发微博的底部约束
    var retweetViewConstraint: Constraint?
    
    var status: CZStatus?{
        didSet{
        
            contentLabel.text = status?.retweeted_status?.text
            
            // 先要写在我们的转发微博底部约束
            self.retweetViewConstraint?.uninstall()
            /*
                判断是否有转发微博配图
                    - status?.retweeted_status?.pic_urls?.count != 0  代表有配图
                    - status?.retweeted_status?.pic_urls?.count == 0  代表没有配图
            */
            // 有转发微博配图
            if status?.retweeted_status?.pic_urls?.count != 0{
                
                // 01 给配图设置数据
                pictureView.pictureInfo = status?.retweeted_status?.pic_urls
                
                
                // 02 更新约束
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.retweetViewConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
                })
                
                // 03 显示配图
                pictureView.hidden = false
                
            }else{
                // 01 没有配图
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.retweetViewConstraint = make.bottom.equalTo(contentLabel).offset(10).constraint
                })
                
                // 02 隐藏配图
                pictureView.hidden = true
            }
            
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        self.backgroundColor = RGBColor(red: 237, green: 237, blue: 237)
        
        // 01- 添加控件
        addSubview(contentLabel)
        addSubview(pictureView)
        // 02- 添加约束
        // 01
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(10)
            make.width.equalTo(kScreenW - 20)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
           // make.size.equalTo(CGSize(width: 100, height: 100))
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
            make.leading.equalTo(contentLabel)
        }
        
        
        self.snp_makeConstraints { (make) -> Void in
            self.retweetViewConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(10).constraint
        }
        
        // 测试
       // contentLabel.text = "转发微博正文转发微博正文转发微博正文转发微博正文转发微博正文转发微博正文转发微博正文"
        
    }
    
    
    // MARK: - 懒加载控件
    // 转发微博正文
    private lazy var contentLabel: UILabel = {
        let lab = UILabel(textColor: UIColor.blackColor(), textFont: 14)
        lab.numberOfLines = 0
        return lab
    }()
    
    // 转发微博配图
    private lazy var pictureView: CZStatusPictureView = CZStatusPictureView()
    
    
    

}
